package workables;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseConnector {
	/**
	 * Default constructor, do not touch
	 */
	public DatabaseConnector()
	{	 try 
		{
	        Class.forName("com.mysql.jdbc.Driver").newInstance();
	    }
	    catch (Exception ex) 
	    {
	       
	    }
	}
	/**
	 * A method to insert a device into the database
	 * @param pinNumberHex the PIN of the BlackBerry Device connecting to the database as converted to a HEX String
	 * @param email the email of the user (might have even more fields)
	 * @return if the insertion was successful 
	 */
	public boolean insertDevice(String pinNumberHex, String email)
	{	try
		{	
			Connection con =DriverManager.getConnection("jdbc:mysql:///blackberry", "root", "dorina");
			java.sql.Statement stat = con.createStatement();
	        String query = "INSERT INTO users (id,email) VALUES(\""+pinNumberHex+"\",\""+email+"\");";
	        stat.executeUpdate(query);
	        con.close();
	        stat.close();
	        con=null;
	        stat=null;
	        query=null;
		    return true;
		}
		catch(Exception ex)
		{	System.out.print(ex.getMessage());
			return false;
		}
		
	}
	/**@deprecated
	 * a void method to return on system.out all lines of the database users
	 */
	
	public void checkTable()
	{
		try 
		{
			Connection con = DriverManager.getConnection("jdbc:mysql:///blackberry", "root", "dorina");
			java.sql.Statement stat = con.createStatement();
	        String query = "SELECT * FROM users;";
	        ResultSet res=stat.executeQuery(query);
	        while(res.next())
	        {
	        	System.out.print(res.getString("id")+" "+res.getString("email")+"\n");
	        }
	        res.close();
	        stat.close();
	        con.close();
	        con=null;
	        stat=null;
	        res=null;
	        query=null;
		} 
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
	}
}
